package com.hzhh123.download;

import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.RemoteViews;

import com.hzhh123.download.utils.notify.NotifyUtils;

public class MainActivity extends AppCompatActivity {
    private static final String TAG = "MainActivity";
    //private DownloadReceiver receiver=new DownloadReceiver();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initUI();
    }

    private void initUI(){
        Button btn1=findViewById(R.id.btn1);
        btn1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                startActivity(new Intent(MainActivity.this,SingleDownloadActivity.class));
            }
        });
        Button btn2=findViewById(R.id.btn2);
        btn2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                startActivity(new Intent(MainActivity.this,MoreDownloadActivity.class));
            }
        });
        Button btn3=findViewById(R.id.btn3);
        btn3.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                startActivity(new Intent(MainActivity.this,LocationActivity.class));
            }
        });
        Button btn4=findViewById(R.id.btn4);
        btn4.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                createNotification();
            }
        });
    }

    /**
     * 发送通知
     */
    private void createNotification(){
        Log.d(TAG, "createNotification: ");
        RemoteViews remoteViews = new RemoteViews(getPackageName(), R.layout.download_notice);
        remoteViews.setProgressBar(R.id.progressBar,100,10,false);
        remoteViews.setTextViewText(R.id.speed,"1.2M/s");
        remoteViews.setTextViewText(R.id.size,"43.2M(10%)");
        NotifyUtils notifyUtils = new NotifyUtils(this, "chanel1","low");
//        IntentFilter filter_click = new IntentFilter();
//        filter_click.addAction("com.hzhh123.notification");
//        //注册广播
//        registerReceiver(receiver, filter_click);
//        Intent Intent_pre = new Intent("com.hzhh123.notification");
//        //得到PendingIntent
//        PendingIntent pendIntent_click = PendingIntent.getBroadcast(this, 0, Intent_pre, 0);
//        //设置监听
//        remoteViews.setOnClickPendingIntent(R.id.pause,pendIntent_click);
        notifyUtils.notifyCustomView(remoteViews,null,
                R.mipmap.word3,0,"hello",9,false,false,false);
    }
    private void createNotification2(){
        Log.d(TAG, "createNotification: ");
        RemoteViews remoteViews = new RemoteViews(getPackageName(), R.layout.download_notice);
        remoteViews.setProgressBar(R.id.progressBar,100,20,false);
        remoteViews.setTextViewText(R.id.speed,"1.5M/s");
        remoteViews.setTextViewText(R.id.size,"43.2M(20%)");
        remoteViews.setImageViewResource(R.id.pause,R.mipmap.start);
        NotifyUtils notifyUtils = new NotifyUtils(this, "chanel1","low1");
//        IntentFilter filter_click = new IntentFilter();
//        filter_click.addAction("com.hzhh123.notification");
//        //注册广播
//        registerReceiver(receiver, filter_click);
//        Intent Intent_pre = new Intent("com.hzhh123.notification");
//        //得到PendingIntent
//        PendingIntent pendIntent_click = PendingIntent.getBroadcast(this, 0, Intent_pre, 0);
//        //设置监听
//        remoteViews.setOnClickPendingIntent(R.id.pause,pendIntent_click);
        notifyUtils.notifyCustomView(remoteViews,null,
                R.mipmap.word3,0,"hello",9,false,false,false);
    }

    class DownloadReceiver extends BroadcastReceiver {
        @Override
        public void onReceive(Context context, Intent intent) {
            if(intent.getAction().equals("com.hzhh123.notification")){
            createNotification2();
            }
        }

    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
//        if(receiver!=null){
//            unregisterReceiver(receiver);
//        }
    }
}
